package fauxman.DynamicElements;

import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.Collection;

import fauxman.Enums.GameElementName;
import fauxman.FauxManStates.FauxManState;
import fauxman.FauxManStates.NormalFauxManState;
import fauxman.StaticElements.PlayableArea;
import fauxman.interfaces.DynamicElement;
import fauxman.interfaces.MoveableElement;
import fauxman.model.WorldModel;
import fauxman.utilities.ActionUtil;
import fauxman.utilities.SpriteUtil;

public class FauxMan extends MoveableElement{
	// TODO: How to animate death scene
	protected FauxManState state;
	protected BufferedImage appearance;
	private WorldModel model;
	private int[] corneringEndDirection = {0,0};
	private int[] corneringEndPoint = {0,0};

	public FauxMan(int conX, int conY, int disX, int disY, int width, int height, WorldModel model){ 
		this.conX = conX;
		this.conY = conY;
		this.disX = disX;
		this.disY = disY;
		this.width = width;
		this.height = height;
		this.conHitbox = new Rectangle(conX+(width/8)*3, conY+(height/8)*4, width/8, height/8);	// Change continuous hitbox to 1 X 1 at (4,4) of a pixel tile.   
		state=NormalFauxManState.getInstance();
		appearance=SpriteUtil.getPacmanSprite(4);
		setCurrentDirection(ActionUtil.WEST);
		this.lookAhead = new Point(disX + 1, disY);
		setNextDirection(getCurrentDirection());
		this.model = model;
		this.gNum = GameElementName.FAUXMAN;
	}
	
	public void draw(Graphics g) {
		state.draw(g, this);
	}

	public FauxManState getState() {
		return state;
	}

	//Used to change the Action ( North, South, East, West) to corner if applicable
	public void setNextAction(int[] action) {
		getState().setNextDirection(action, model, this);
	}
	
	public void setState(FauxManState state) {
		this.state = state;
	}
	
	public BufferedImage getAppearance() {
		return appearance;
	}

	public void setAppearance(BufferedImage appearance) {
		this.appearance = appearance;
	}
	
	public boolean isCollisionNecessary(){
		return true;
	}
	
	public void collide(WorldModel model, DynamicElement de){
		state.collide(model,de);
	}
	
	public String toString(){
		return "FM";
	}
	
	public Collection<? extends PlayableArea> getPlayableArea(){
		return getState().getPlayableArea(model);//model.getPlayableArea();
	}

	public void setCorneringTrailEnd(Point endP) {
		corneringEndPoint[0] = endP.x;
		corneringEndPoint[1] = endP.y;
	}

	public void setCorneringEndDirection(int[] action) {
		corneringEndDirection[0]= action[0];
		corneringEndDirection[1] = action[1];
	}
	
	public int[] getCorneringEndDirection(){
		return corneringEndDirection;
	}
	
	public int[] getCorneringTrailEnd(){
		return corneringEndPoint;
	}

	@Override
	public int[] reactToCurrentContinuousPosition(){
		return getState().reactToCurrentContinuousPosition(model,this);
	}

}
